//
//  PublicityViewController.m
//  iStore
//
//  Created by Geek on 15/4/22.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "AccountViewController.h"
#import "RequestEngine.h"
#import "AccountTableViewCell.h"
#import "ADModel.h"

@interface AccountViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_array;
}
@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账本";
    NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:kUserId];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsSelection = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    _array = [[NSMutableArray alloc]init];

    [self activityStart];
    [RequestEngine RequestEngineWithDic:@{@"userID":userID} URL:@"queryAccountBook" completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        if ([errorcode isEqualToString:@"0"]) {
            _array = resultDict[@"ADAccountBook"];
            
            [self createAccountBook:resultDict];
            [_tableView reloadData];
            if (_array.count == 0 || _array == nil) {
                [self createNull];
            }
        }
        
    }];
 
}
- (void)createNull
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 170, ScreenWidth, 40)];
    label.text = @"您还没有账本信息喔~";
    label.textColor = [UIColor blackColor];
    [label setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:label];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AccountTableViewCell" owner:self options:nil]firstObject];
    }
    [cell refreshData:[_array objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)createAccountBook:(NSDictionary*)resultDict
{
    _adverSum.text=[NSString stringWithFormat:@"%.2f",[[resultDict objectForKey:@"adverSumCost"] floatValue]];
    _listenedSum.text=[NSString stringWithFormat:@"%@",[resultDict objectForKey:@"listenedSum"]];
    _balance.text=[NSString stringWithFormat:@"%.2f",[[resultDict objectForKey:@"balance"] floatValue]];
    _costSum.text=[NSString stringWithFormat:@"%.2f",[[resultDict objectForKey:@"costSum"] floatValue]];
}

@end
